package com.codeyang.controller;

import com.codeyang.domain.User;
import com.codeyang.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 描述: TO-删除标志-DO
 *
 * @author CodeYang_Site
 * @version 2021/5/27 12:32
 */
@Api("前台用户接口")
@RestController
@RequestMapping("p/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PutMapping("setUserInfo")
    @ApiOperation("更新用户信息的接口")
    public ResponseEntity<Void> setUserInfo(@RequestBody User user) {
        //1.拿到当前登录用户
        //userId ---> openId
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        user.setUserId(userId);
        userService.updateById(user);
        return ResponseEntity.ok().build();
    }


    @GetMapping("getUserInfoById")
    @ApiOperation("获取用户的信息")
    public User getUserInfo(String openId) {
        return userService.getById(openId);
    }


    /**
     * 远程调用 可以查看 Client Feign 108行
     *
     * @param userIds
     * @return
     */
    @PostMapping("getUsersByIds")
    @ApiOperation("根据用户ids查询用户信息")
    public List<User> getUsersByIds(@RequestBody Set<String> userIds) {
        return userService.listByIds(userIds);
    }
}
